package com.dorm.service;

import com.dorm.entity.HygieneRecord;
import com.dorm.mapper.HygieneMapper;
import com.dorm.mapper.RoomMapper;
import com.dorm.util.Debug;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.ArrayList;

@Service
public class HygieneService {
    private final HygieneMapper hygieneMapper;
    private final RoomMapper roomMapper;

    @Autowired
    public HygieneService(HygieneMapper hygieneMapper, RoomMapper roomMapper) {
        this.hygieneMapper = hygieneMapper;
        this.roomMapper = roomMapper;
    }

    public List<HygieneRecord> getByRoom(Long roomId) {
        try {
            Debug.log("开始获取房间卫生记录, roomId: " + roomId);
            List<HygieneRecord> records = hygieneMapper.findByRoomId(roomId);
            if (records == null) {
                Debug.log("房间卫生记录为null，返回空列表");
                return new ArrayList<>();
            }
            Debug.log("获取到房间卫生��录数量: " + records.size());
            return records;
        } catch (Exception e) {
            Debug.error("获取房间卫生记录失败:", e);
            throw new RuntimeException("获取房间卫生记录失败: " + e.getMessage());
        }
    }

    @Transactional
    public HygieneRecord create(HygieneRecord record) {
        hygieneMapper.insert(record);
        return record;
    }

    @Transactional
    public void delete(Long id) {
        hygieneMapper.deleteById(id);
    }

    public Double getAverageScoreByBuilding(Long buildingId) {
        return hygieneMapper.getAverageScoreByBuilding(buildingId);
    }

    public Integer getCheckedRoomCountByBuilding(Long buildingId) {
        return hygieneMapper.getCheckedRoomCountByBuilding(buildingId);
    }

    public String getLastCheckTimeByBuilding(Long buildingId) {
        return hygieneMapper.getLastCheckTimeByBuilding(buildingId);
    }
} 